package cn.lingyangwl.framework.core.response.format;

import cn.lingyangwl.framework.core.response.RequestHandler;
import cn.lingyangwl.framework.core.utils.spring.SpringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author shenguangyang
 */
public class ResponseFormatHandler {
    @Resource
    private RequestHandler requestHandler;

    private static ResponseFormat defaultResponseFormat;

    /**
     * 默认api响应格式标签
     */
    @Resource
    private ResponseFormat responseFormat;

    @PostConstruct
    public void init() {


        defaultResponseFormat = new ResponseFormat(this.responseFormat.getResponseCode());
    }

    public static ResponseFormat getDefaultResponseFormat() {
        if (Objects.isNull(defaultResponseFormat)) {
            synchronized (ResponseFormat.class) {
                if (Objects.nonNull(defaultResponseFormat)) {
                    return defaultResponseFormat;
                }
                defaultResponseFormat = new ResponseFormat(new ResponseCode());
            }
        }
        return defaultResponseFormat;
    }

    /**
     * 获取格式, 只要是网关就走自定义的响应体
     */
    public static ResponseFormat getFormat() {
        ApplicationContext context = SpringUtils.getContext();
        if (Objects.isNull(context)) {
            return ResponseFormatHandler.getDefaultResponseFormat();
        }

        ResponseFormatHandler instance = SpringUtils.getBean(ResponseFormatHandler.class);

//        // 如果是REACTIVE环境强制采用自定义的响应格式(对外)
//        if (WebType.REACTIVE.equals(instance.requestHandler.getWebType())) {
//            return instance.responseFormat;
//        }

        for (Map.Entry<String, Set<String>> entry : ResponseFormat.noWrapFormatHeaderMap.entrySet()) {
            String headerValue = instance.requestHandler.getHeader(entry.getKey());
            // 判断是否采用Resp类响应格式
            if (entry.getValue().stream().anyMatch(e -> e.equalsIgnoreCase(headerValue))) {
                return defaultResponseFormat;
            }
        }

        return instance.responseFormat;
    }

    /**
     * 获取格式
     */
    public static ResponseFormat getFormat(HttpHeaders httpHeaders) {
        ApplicationContext context = SpringUtils.getContext();
        if (Objects.isNull(context)) {
            return ResponseFormatHandler.getDefaultResponseFormat();
        }

        ResponseFormatHandler instance = SpringUtils.getBean(ResponseFormatHandler.class);

        for (Map.Entry<String, Set<String>> entry : ResponseFormat.noWrapFormatHeaderMap.entrySet()) {
            String headerValue = httpHeaders.getFirst(entry.getKey());
            // 判断是否采用Resp类响应格式
            if (entry.getValue().stream().anyMatch(e -> e.equalsIgnoreCase(headerValue))) {
                return defaultResponseFormat;
            }
        }

        return instance.responseFormat;
    }
}
